package org.zakariya.flyoutmenudemo.menus;

import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import org.zakariya.flyoutmenu.FlyoutMenuView;


public class IconFlyoutMenu {
    static class IconDrawer {

        Element icon;
        float inset;
        Path clipPath;
        RectFloat previousBounds;
        float alpha;

        IconDrawer(Element icon, float inset) {
            this.icon = icon;
            this.inset = inset;
            this.previousBounds = new RectFloat();
            setAlpha(1);
        }

        Element getIcon() {
            return icon;
        }

        void setIcon(Element icon) {
            this.icon = icon;
        }

        float getAlpha() {
            return alpha;
        }

        void setAlpha(float alpha) {
            this.alpha = Math.min(Math.max(alpha, 0), 1);
        }

        public float getInset() {
            return inset;
        }

        public void setInset(float inset) {
            this.inset = inset;
        }

        void draw(Canvas canvas, RectFloat bounds) {
            bounds = new RectFloat(bounds);
            bounds.isInclude(inset, inset);  //inset
            canvas.save();

            if (clipPath == null || !bounds.equals(previousBounds)) {
                previousBounds = bounds;
                clipPath = new Path();
                clipPath.addCircle(bounds.getCenter().position[0], bounds.getCenter().position[1], Math.min(bounds.getWidth(), bounds.getHeight()) / 2, Path.Direction.COUNTER_CLOCK_WISE);
            }

            canvas.clipPath(clipPath,Canvas.ClipOp.INTERSECT);

            float availableWidth = bounds.getWidth();
            float availableHeight = bounds.getHeight();

            float width = icon.getWidth();  //获取图像的高宽 dp为单位
            if (width < 0) {width = availableWidth;}

            float height = icon.getHeight();
            if (height < 0) {height = bounds.getHeight();}

            if (width > availableWidth) {
                float scale = availableWidth / width;
                width *= scale;
                height *= scale;
            }

            if (height < availableHeight) {
                float scale = availableHeight / height;
                width *= scale;
                height *= scale;
            }

            icon.setBounds(
                    (int) (bounds.getCenter().position[0] - width / 2),
                    (int) (bounds.getCenter().position[1] - height / 2),
                    (int) (bounds.getCenter().position[0] + width / 2),
                    (int) (bounds.getCenter().position[1] + height / 2)
            );

            icon.setAlpha((int) (this.alpha * 255f));
            icon.drawToCanvas(canvas);

            canvas.restore();
        }
    }

    public static class MenuItem extends FlyoutMenuView.MenuItem {

        IconDrawer iconDrawer;

        public MenuItem(Context context,int id, Element icon, float inset) {
            super(context,id);
            iconDrawer = new IconDrawer(icon, inset);
            iconDrawer.setAlpha(1);
        }

        public Element getIcon() {
            return iconDrawer.getIcon();
        }

        @Override
        public void onDraw(Canvas canvas, RectFloat bounds, float degreeSelected) {
            iconDrawer.draw(canvas, bounds);
        }
    }

    public static class ButtonRenderer extends FlyoutMenuView.ButtonRenderer {

        IconDrawer iconDrawer;

        public ButtonRenderer(Element drawable, float inset) {
            super();
            iconDrawer = new IconDrawer(drawable, inset);
        }

        public void setIcon(Element icon) {
            iconDrawer.setIcon(icon);
        }

        public Element getIcon() {
            return iconDrawer.getIcon();
        }

        @Override
        public void onDrawButtonContent(Canvas canvas, RectFloat buttonBounds, Color buttonColor, float alpha) {
            iconDrawer.setAlpha(alpha);
            RectFloat bounds = new RectFloat(buttonBounds.left-40, buttonBounds.top - 40, buttonBounds.right+40, buttonBounds.bottom + 40);
            iconDrawer.draw(canvas, bounds);
        }
    }
}
